package com.liuym.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Liuym
 * @date 2018/12/12 17:12
 * @describe：日期操作类
 */
public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);


    /**
     * 字符串转Date
     *
     * @param date
     * @param format
     * @return
     */
    public static Date stringToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            logger.error("日期转换异常：", e);
        }
        return null;
    }

    /**
     * Date转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String DateToString(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }
}
